package com.telecomyt.jwportal.activity.collect;

import android.content.Context;
import android.content.Intent;
import android.view.View;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.telecomyt.jwportal.R;
import com.telecomyt.jwportal.activity.collect.adapter.MyCollectAdapter;
import com.telecomyt.jwportal.activity.collect.bean.CollectBean;
import com.telecomyt.jwportal.activity.person.PersonalDetailsActivity;
import com.telecomyt.jwportal.base.BaseActivity;
import com.telecomyt.jwportal.constant.AppConfig;
import com.telecomyt.jwportal.utils.DataCleanManager;
import com.telecomyt.jwportal.widget.FraToolBar;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * @date: 2020/9/8
 * @author: chengDa
 * @description: 我的收藏
 */
public class MyCollectActivity extends BaseActivity<CollectPresenter> implements CollectContract.View {

    @BindView(R.id.mFraToolBar)
    FraToolBar mFraToolBar;
    @BindView(R.id.mRecyclerView)
    RecyclerView mRecyclerView;
    @BindView(R.id.mSwipeRefreshLayout)
    SwipeRefreshLayout mSwipeRefreshLayout;
    private MyCollectAdapter myCollectAdapter;
    List<CollectBean.RecordsBean.UserCenterBean> lisBeans = new ArrayList<>();

    public static void start(Context context) {
        Intent starter = new Intent(context, MyCollectActivity.class);
        context.startActivity(starter);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_my_collect;
    }

    @Override
    protected void initView() {
        mFraToolBar.setLeftFinish(this);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        myCollectAdapter = new MyCollectAdapter(R.layout.activity_my_collect_items);
        mRecyclerView.setAdapter(myCollectAdapter);
        myCollectAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                if (myCollectAdapter.getData().get(position).getDepts() != null && myCollectAdapter.getData().get(position).getDepts().size() > 0) {
                    PersonalDetailsActivity.start(MyCollectActivity.this, myCollectAdapter.getData().get(position).getUserId()
                            , myCollectAdapter.getData().get(position).getDepts().get(0).getTenantId());
                }

            }
        });
        mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                DataCleanManager.cleanFiles(MyCollectActivity.this);
                persenter.getCollectData(1459, 110, AppConfig.REFRESH);
            }
        });
    }

    @Override
    protected void initData() {
        persenter.getCollectData(1459, 110, AppConfig.REFRESH);
    }

    @Override
    protected CollectPresenter creatPresenter() {
        return new CollectPresenter(this);
    }

    @Override
    public void setCollectData(CollectBean bean, int requestCode) {
        if (requestCode == AppConfig.REFRESH) {
            mSwipeRefreshLayout.setRefreshing(false);
        }
        lisBeans.clear();
        List<CollectBean.RecordsBean> records = bean.getRecords();
        for (int i = 0; i < records.size(); i++) {
            lisBeans.add(records.get(i).getUserCenter());
        }
        myCollectAdapter.setNewData(lisBeans);
    }


}